qui {
	local subsample = "$subsample"
	local modelname = "$modelname"
	****************************************************************************************************************
	foreach rhs in $RHSlist { 
		local cnt=0
		forvalues Kpaper=$K_list {
			foreach lhs in $LHSlist { 
				foreach robust in $robust_list {
					local cnt=`cnt'+1
					use "$datapath/004_het_beta3_delta1_flex_delta_results_`subsample'_`modelname'_`lhs'_`robust'_K`Kpaper'.dta", clear
					drop RHS LHS Kcluster Robust 
					merge 1:1 _n using "$datapath/004_het_beta3_delta1_flex_delta_results_`subsample'_`modelname'_`lhs'_`robust'_K`Kpaper'BOOT.dta"
					drop _merge

					if `cnt'==1 {
						tempfile temp_`rhs'
						save `temp_`rhs'', replace
					}
					else {
						append using `temp_`rhs''
						save `temp_`rhs'', replace		
					}
				}
			}
		}
	}

	// Make Tables
	*local list="NaiveN Naive Tobit HetTobit HetSymm"		
	*local shortlist="Tobit HetTobit HetSymm"
	local list="none naive symm hettobit hetuni"		
	local shortlist="symm hettobit hetuni"		

	foreach rhs in $RHSlist { 
		if "`rhs'"=="avg_weeks_worked_3years" {
			local factor=20
		}
		else {
			local factor=100
		}
		forvalues Kpaper=$K_list {
			// beta const
			local cnt=0
			foreach lhs in $LHSlist { 
				foreach var in `list' {
					local cnt=`cnt'+1
					use `temp_`rhs'', clear
					keep if LHS=="`lhs'" // must have one observation only after this restriction
					keep const_`var' sd_const_`var'
					gen t_const_`var'=abs(const_`var'/sd_const_`var')
					replace const_`var' = round(const_`var'*`factor',0.0001)
					replace sd_const_`var' = round(sd_const_`var'*`factor',0.0001)
					rename const_`var' beta_`lhs'
					rename sd_const_`var' betase_`lhs'
					rename t_const_`var' t_`lhs'	
					order beta_`lhs' betase_`lhs' t_`lhs'
					xpose, clear varname	
					gen id2=_n	
					if v1[3]>=1.96 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"**" in 1
						replace v1="("+v1+")   " in 2
					}
					else if v1[3]>=1.645 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"*" in 1
						replace v1="("+v1+")   " in 2
					}
					else {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"" in 1
						replace v1="("+v1+")   " in 2	
					}
					order _varname, first
					rename v1 `var'
					gen LHS="`lhs'"	
					drop if _varname=="t_`lhs'"
					gen coeff="betabase"
					if `cnt'==1 {
						tempfile temp1
						save `temp1', replace
					}
					else {
						merge 1:1 _varname using `temp1'
						drop _merge
						save `temp1', replace		
					}
				}
			}
			tempfile tempA
			save `tempA', replace

			// beta beta1
			local cnt=0
			foreach lhs in $LHSlist { 
				foreach var in `list' {
					local cnt=`cnt'+1
					use `temp_`rhs'', clear

					keep if LHS=="`lhs'" // must have one observation only after this restriction
					keep beta1_`var' sd_beta1_`var'
					gen t_beta1_`var'=abs(beta1_`var'/sd_beta1_`var')
					replace beta1_`var' = round(beta1_`var'*`factor',0.0001)
					replace sd_beta1_`var' = round(sd_beta1_`var'*`factor',0.0001)
					rename beta1_`var' beta1_`lhs'
					rename sd_beta1_`var' beta1se_`lhs'
					rename t_beta1_`var' t_`lhs'	
					order beta1_`lhs' beta1se_`lhs' t_`lhs'
					xpose, clear varname	
					gen id2=_n	
					if v1[3]>=1.96 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"**" in 1
						replace v1="("+v1+")   " in 2
					}
					else if v1[3]>=1.645 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"*" in 1
						replace v1="("+v1+")   " in 2
					}
					else {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"" in 1
						replace v1="("+v1+")   " in 2	
					}
					order _varname, first
					rename v1 `var'
					gen LHS="`lhs'"	
					drop if _varname=="t_`lhs'"
					gen coeff="beta1"
					if `cnt'==1 {
						tempfile temp1
						save `temp1', replace
					}
					else {
						merge 1:1 _varname using `temp1'
						drop _merge
						save `temp1', replace		
					}
				}
			}
			tempfile tempAbeta1
			save `tempAbeta1', replace

			// beta beta2
			local cnt=0
			foreach lhs in $LHSlist { 
				foreach var in `list' {
					local cnt=`cnt'+1
					use `temp_`rhs'', clear

					keep if LHS=="`lhs'" // must have one observation only after this restriction
					keep beta2_`var' sd_beta2_`var'
					*multiple hours by 1000 
					if ("`modelname'"=="table3_main_flexdelta"){
						replace beta2_`var' = beta2_`var'*1000
						replace sd_beta2_`var' = sd_beta2_`var'*1000
					}
					if ("`modelname'"=="table3_wagebefore_flexdelta"){
						replace beta2_`var' = beta2_`var'*1000
						replace sd_beta2_`var' = sd_beta2_`var'*1000
					}
					if ("`modelname'"=="table3_education_flexdelta"){
						replace beta2_`var' = beta2_`var'*1000
						replace sd_beta2_`var' = sd_beta2_`var'*1000
					}
					gen t_beta2_`var'=abs(beta2_`var'/sd_beta2_`var')
					replace beta2_`var' = round(beta2_`var'*`factor',0.0001)
					replace sd_beta2_`var' = round(sd_beta2_`var'*`factor',0.0001)
					rename beta2_`var' beta2_`lhs'
					rename sd_beta2_`var' beta2se_`lhs'
					rename t_beta2_`var' t_`lhs'	
					order beta2_`lhs' beta2se_`lhs' t_`lhs'
					xpose, clear varname	
					gen id2=_n	
					if v1[3]>=1.96 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"**" in 1
						replace v1="("+v1+")   " in 2
					}
					else if v1[3]>=1.645 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"*" in 1
						replace v1="("+v1+")   " in 2
					}
					else {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"" in 1
						replace v1="("+v1+")   " in 2	
					}
					order _varname, first
					rename v1 `var'
					gen LHS="`lhs'"	
					drop if _varname=="t_`lhs'"
					gen coeff="beta2"
					if `cnt'==1 {
						tempfile temp1
						save `temp1', replace
					}
					else {
						merge 1:1 _varname using `temp1'
						drop _merge
						save `temp1', replace		
					}
				}
			}
			tempfile tempAbeta2
			save `tempAbeta2', replace

			// beta beta3
			local cnt=0
			foreach lhs in $LHSlist { 
				foreach var in `list' {
					local cnt=`cnt'+1
					use `temp_`rhs'', clear

					keep if LHS=="`lhs'" // must have one observation only after this restriction
					keep beta3_`var' sd_beta3_`var'
					*Multiple hours terms by 1000
					if ("`modelname'"=="table3_main_flexdelta"){
						replace beta3_`var' = beta3_`var'*1000
						replace sd_beta3_`var' = sd_beta3_`var'*1000
					}
					if ("`modelname'"=="table3_wagebefore_flexdelta"){
						replace beta3_`var' = beta3_`var'*1000
						replace sd_beta3_`var' = sd_beta3_`var'*1000
					}
					if ("`modelname'"=="table3_education_flexdelta"){
						replace beta3_`var' = beta3_`var'*1000
						replace sd_beta3_`var' = sd_beta3_`var'*1000
					}
						
					gen t_beta3_`var'=abs(beta3_`var'/sd_beta3_`var')
					replace beta3_`var' = round(beta3_`var'*`factor',0.0001)
					replace sd_beta3_`var' = round(sd_beta3_`var'*`factor',0.0001)
					rename beta3_`var' beta3_`lhs'
					rename sd_beta3_`var' beta3se_`lhs'
					rename t_beta3_`var' t_`lhs'	
					order beta3_`lhs' beta3se_`lhs' t_`lhs'
					xpose, clear varname	
					gen id2=_n	
					if v1[3]>=1.96 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"**" in 1
						replace v1="("+v1+")   " in 2
					}
					else if v1[3]>=1.645 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"*" in 1
						replace v1="("+v1+")   " in 2
					}
					else {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"" in 1
						replace v1="("+v1+")   " in 2	
					}
					order _varname, first
					rename v1 `var'
					gen LHS="`lhs'"	
					drop if _varname=="t_`lhs'"
					gen coeff="beta3"
					if `cnt'==1 {
						tempfile temp1
						save `temp1', replace
					}
					else {
						merge 1:1 _varname using `temp1'
						drop _merge
						save `temp1', replace		
					}
				}
			}
			tempfile tempAbeta3
			save `tempAbeta3', replace

			// delta const
			local cnt=0
			foreach lhs in $LHSlist { 
				foreach var in `shortlist' {
					local cnt=`cnt'+1
					use `temp_`rhs'', clear
					keep if LHS=="`lhs'" // must have one observation only after this restriction
					keep d_const_`var' sd_d_const_`var'
					gen t_`var'=abs(d_const_`var'/sd_d_const_`var')
					replace d_const_`var' = round(d_const_`var'*`factor',0.0001)
					replace sd_d_const_`var' = round(sd_d_const_`var'*`factor',0.0001)
					rename d_const_`var' delta_const_`lhs'
					rename sd_d_const_`var' deltase_const_`lhs'
					rename t_`var' t_`lhs'	
					order delta_const_`lhs' deltase_const_`lhs' t_`lhs'
					xpose, clear varname	
					gen id2=_n	
					if v1[3]>=1.96 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"**" in 1
						replace v1="("+v1+")   " in 2
					}
					else if v1[3]>=1.645 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"*" in 1
						replace v1="("+v1+")   " in 2
					}
					else {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"" in 1
						replace v1="("+v1+")   " in 2	
					}
					order _varname, first
					rename v1 `var'
					gen LHS="`lhs'"	
					drop if _varname=="t_`lhs'"
					gen coeff="delta_const"
					if `cnt'==1 {
						tempfile temp1
						save `temp1', replace
					}
					else {
						merge 1:1 _varname using `temp1'
						drop _merge
						save `temp1', replace		
					}
				}
			}
			tempfile tempB
			save `tempB', replace

			// delta coc
			local cnt=0
			foreach lhs in $LHSlist { 
				foreach var in `shortlist' {
					local cnt=`cnt'+1
					use `temp_`rhs'', clear
					keep if LHS=="`lhs'" // must have one observation only after this restriction
					keep d_delta1_`var' sd_d_delta1_`var'
					gen t_`var'=abs(d_delta1_`var'/sd_d_delta1_`var')
					replace d_delta1_`var' = round(d_delta1_`var'*`factor',0.0001)
					replace sd_d_delta1_`var' = round(sd_d_delta1_`var'*`factor',0.0001)
					rename d_delta1_`var' delta_delta1_`lhs'
					rename sd_d_delta1_`var' deltase_delta1_`lhs'
					rename t_`var' t_`lhs'	
					order delta_delta1_`lhs' deltase_delta1_`lhs' t_`lhs'
					xpose, clear varname	
					gen id2=_n	
					if v1[3]>=1.96 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"**" in 1
						replace v1="("+v1+")   " in 2
					}
					else if v1[3]>=1.645 {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"*" in 1
						replace v1="("+v1+")   " in 2
					}
					else {
						tostring v1, replace force format(%05.3f)
						replace v1=v1+"" in 1
						replace v1="("+v1+")   " in 2	
					}
					order _varname, first
					rename v1 `var'
					gen LHS="`lhs'"	
					drop if _varname=="t_`lhs'"
					gen coeff="delta_delta1"
					if `cnt'==1 {
						tempfile temp1
						save `temp1', replace
					}
					else {
						merge 1:1 _varname using `temp1'
						drop _merge
						save `temp1', replace		
					}
				}
			}
			tempfile tempB_delta1
			save `tempB_delta1', replace


			use `tempA', clear
			append using `tempAbeta1'
			append using `tempAbeta2'
			append using `tempAbeta3'
			append using `tempB'
			append using `tempB_delta1'
			sort LHS coeff id2
			order LHS coeff id2
			*don't want the bunching, since not sig
			*drop if coeff == "betatopb"

			replace _varname=""
			*replace _varname="Cognitive" if LHS=="cog" & id2==1 & coeff=="betabase"
			*replace _varname="Non-Cognitive" if LHS=="noncog" & id2==1 & coeff=="betabase"

			replace coeff="Baseline" if coeff=="betabase" & id==1
			replace coeff="Beta1" if coeff=="beta1" & id==1
			replace coeff="Beta2" if coeff=="beta2" & id==1
			replace coeff="Beta3" if coeff=="beta3" & id==1
			*replace coeff="Top Bunching" if coeff=="betatopb" & id==1
			drop if coeff=="delta_const" 
			*replace coeff="Delta0" if coeff=="delta_const" & id==1 
			replace coeff="Delta1" if coeff=="delta_delta1" & id==1 
			replace coeff="" if id==2
			label variable _varname ""
			label variable symm "Het. Symmetric"
			label variable hettobit "Het. Tobit"
			label variable hetuni"Het. Uniform"
			label variable none "No Controls"
			label variable naive "Controls"
			order _varname coeff none naive hettobit hetuni symm
			gen counter = _n
			gen counter2  = .
			replace counter2 = 1 if counter == 7
			replace counter2 = 2 if counter == 8
			replace counter2 = 3 if counter == 1
			replace counter2 = 4 if counter == 2
			replace counter2 = 5 if counter == 3
			replace counter2 = 6 if counter == 4
			replace counter2 = 7 if counter == 5
			replace counter2 = 8 if counter == 6
			sort counter2 counter
			drop counter*
			
			texsave _varname coeff none naive hettobit hetuni symm using "$outpath/005_het_beta3_delta1_flex_delta_estimates_`subsample'_`modelname'_`rhs'_K`Kpaper'.tex", replace ///
				varlabels hlines(8) size(5) align(cc|CCCCC) rowsep(.05in) ///
				headerlines("& & (i) & (ii) & (iii) & (iv) & (v)") //frag
		}
	}
}
